<?php

namespace App\Admin\Controllers\SysManage;


use App\Importer\Basics\BImporter;
use App\Models\Basics\SysCountry;
use App\Models\Product\ProductAmazonSku;
use App\Models\Product\ProductSku;
use App\Util\RExcel;
use Encore\Admin\Controllers\AdminController;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class TestExcelUploadController extends AdminController{

    protected $title = '上传测试Excel';

    protected function grid(){
        $type = request('type');
        return view("sys-manage.test-excel-upload.index", compact('type'));
    }

    public function upload(Request $request){

        //$file = base_path('data.xlsx');
        //$lines = RExcel::importSheet($file);
        //unset($lines[0]);
        //echo "<pre>";
        //print_r($lines);
        //echo "</pre>";
        //exit;

        $file = $request->file('source_file');
        if ($file->isValid()) {
            $action = $request->get('action');
            $re = $this->{'_import'.$action}($file);
            if(is_array($re)) {
                if($re['flag']){
                    admin_toastr('文件：'.$re['data'].' 上传成功！');
                }else{
                    admin_toastr($re['data'],'error');
                }
            }else{
                return $re;
            }
        }else{
            admin_toastr('无效文件！','error');
        }
        return back()->withInput();
    }

    //验证亚马逊平台sku(是否存在)
    private function _importPlatSku(UploadedFile $file){

        try{
            $lines = RExcel::importSheet1($file);
            unset($lines[0]);
            dd($lines);

            $data = Excel::toArray(new BImporter(),$file);
            $data = $data[0];
            if(!$data)return ['flag' => false,'data' => '请上传包含有数据的文件！'];
            $errors = [];
            $count = 1;
            foreach($data as $row){
                $count++;
                $_country = $row[0] ? trim($row[0]) : '';
                $_sku = $row[1] ? trim($row[1]) : '';
                $_sku1 = $row[6] ? trim($row[6]) : '';
                dd($row);


                $countryCode = strtoupper($_country);
                $country = SysCountry::query()->where(['country'=>$countryCode])->first();
                //if(!$country){
                //    $errors[$count] = $_country.',国家不存在';
                //}

                $skuCode = strtoupper($_sku);
                $productSku = ProductSku::query()->where(['psku_code'=>$skuCode])->first();
                //if(!$productSku){
                //    $errors[$count] = $_sku.',公司产品不存在';
                //}

                $productAmazonSku = ProductAmazonSku::query()->where([
                    'country_id' => $country->id ?? 0,
                    'psku_id' => $productSku->id ?? 0,
                    'status' => ProductAmazonSku::STATUS_YES,
                ])->first();
                $errors[$count] = $productAmazonSku->seller_sku ?? ' ';
            }

            if(empty($errors)){
                return ['flag' => true,'data' => $file->getClientOriginalName()];
            }else{
                return RExcel::importResult($errors);
            }
        }catch(\Exception $e){
            report($e);
            return ['flag' => false,'data' => '系统异常！'];
        }
    }

    //public function template(Request $request){
    //    $temp = $request->get('temp');
    //    $suffix = $request->get('suffix','xlsx');
    //    return Storage::disk('admin')->download("template/{$temp}-template.{$suffix}");
    //}

}
